package com.open.capacity.finance.service.impl;

import com.open.capacity.finance.dao.AuditModelDao;
import com.open.capacity.finance.dto.AuditModelAuthorizes;
import com.open.capacity.finance.entity.AuditModel;
import com.open.capacity.finance.util.uuid.UUIDUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.open.capacity.commons.PageResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.open.capacity.commons.CodeEnum;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections4.MapUtils;

import com.open.capacity.finance.entity.AuditModelWarnScope;
import com.open.capacity.finance.dao.AuditModelWarnScopeDao;
import com.open.capacity.finance.service.AuditModelWarnScopeService;
import org.springframework.transaction.annotation.Transactional;


@Service
public class AuditModelWarnScopeServiceImpl implements AuditModelWarnScopeService {

    @Autowired
    private AuditModelWarnScopeDao auditModelWarnScopeDao;

    @Autowired
    private AuditModelDao auditModelDao;

    /**
     * 添加
     *
     * @param auditModelWarnScopes
     */
    @Transactional
    public int save(AuditModelAuthorizes auditModelWarnScopes) {
        auditModelWarnScopeDao.deleteByModelId(auditModelWarnScopes.getModelId());
        for (AuditModelWarnScope item : auditModelWarnScopes.getAuditAuthorizes()) {
            item.setId(UUIDUtils.getOriginalUUIDWithoutBar());
            item.setUpdateTime(new Date());
            auditModelWarnScopeDao.save(item);
        }

        //保存告警配置
        AuditModel model = auditModelDao.findById(auditModelWarnScopes.getModelId());
        model.setRectifyPeriod(auditModelWarnScopes.getRectifyPeriod());
        model.setWarnNeed(auditModelWarnScopes.getWarnNeed());
        model.setCronExpression(auditModelWarnScopes.getCronExpression());
        model.setDefaultKjnd(auditModelWarnScopes.getDefaultKjnd());
        auditModelDao.update(model);

        return 1;
    }


    /**
     * 删除
     *
     * @param id
     */
    public int delete(String id) {
        return auditModelWarnScopeDao.delete(id);
    }

    /**
     * 查询记录
     *
     * @param modelId
     * @return
     */
    public AuditModelAuthorizes findById(String modelId) {
        AuditModelAuthorizes data = new AuditModelAuthorizes();
        List<AuditModelWarnScope> list = auditModelWarnScopeDao.findByModelId(modelId);
        data.setAuditAuthorizes(list);
        data.setModelId(modelId);

        AuditModel model = auditModelDao.findById(modelId);
        data.setCronExpression(model.getCronExpression());
        data.setRectifyPeriod(model.getRectifyPeriod());
        data.setWarnNeed(model.getWarnNeed());
        data.setDefaultKjnd(model.getDefaultKjnd());

        return data;
    }


    /**
     * 分页
     *
     * @param params
     * @return
     */
    public PageResult<AuditModelWarnScope> pageAll(Map<String, Object> params) {
        //设置分页信息，分别是当前页数和每页显示的总记录数【记住：必须在mapper接口中的方法执行之前设置该分页信息】
        if (MapUtils.getInteger(params, "page") != null && MapUtils.getInteger(params, "limit") != null)
            PageHelper.startPage(MapUtils.getInteger(params, "page"), MapUtils.getInteger(params, "limit"), true);

        List<AuditModelWarnScope> list = auditModelWarnScopeDao.findAll(params);
        PageInfo<AuditModelWarnScope> pageInfo = new PageInfo(list);

        return PageResult.<AuditModelWarnScope>builder().data(pageInfo.getList()).resp_code(CodeEnum.SUCCESS.getCode()).count(pageInfo.getTotal()).build();
    }

    /**
     * 列表
     *
     * @param params
     * @return
     */
    public List<AuditModelWarnScope> findAll(Map<String, Object> params) {
        return auditModelWarnScopeDao.findAll(params);
    }

}
